package animator.phantom.renderer.plugin;

/*
    Copyright Janne Liljeblad

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.plugin.*;
import animator.phantom.renderer.*;
import animator.phantom.renderer.param.*;
import animator.phantom.paramedit.*;
import animator.phantom.gui.view.*;
import animator.phantom.gui.view.editlayer.*;
import animator.phantom.renderer.threedee.TexturedRect3D;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.image.*;
import giotto2D.core.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.util.*;
import animator.phantom.renderer.plugin.editlayer.FileSource3DPluginEditLayer;


public class FileSource3DPlugin extends Phantom3DPlugin
{

	public FileSource3DPlugin()
	{
		init3DPlugin();
	}

	public void buildDataModel()
	{
		setName( "3DImageSource" );

		register3DCoords();
	}

	public void buildEditPanel()
	{
		add3DCoordsEditors();
	}

	public void renderFullScreenMovingSource( float frameTime, Graphics2D g, int width, int height )
	{
		int frame = Math.round( frameTime );
		BufferedImage img = getFileSourceImage( frame );

		AnimCoords3D coords = get3DCoords();

		//--- Create new appearance if neede. appearance
		if( getAppearance() == null || fileSourceImageChangesPerFrame() )
			setTexturedAppearance( img );

		//--- Create new shape.
		//--- NOTE: Because same Shape3D cannot be reused after once connect to universe via group
		//--- and we don't want to create new universe for every frame we must destroy used Shape3D objects in universe
		//--- so that they don't show in other images and create new Shape3D objects for each frame.
		QuadArray polygon = getTexturePolygon( img );
		Shape3D rect = new Shape3D ( polygon, getAppearance() );

		//--- Render image 
		BufferedImage rendered3DImg = render3DImage( frameTime, coords, rect, PluginUtils.getScreenSize() );

		//--- render and combine alpha
		QuadArray alphaPolygon = getTexturePolygon( img );
		Appearance whiteApp = getWhiteAppearance();
		Shape3D alphaRect = new Shape3D( alphaPolygon, whiteApp );
		BufferedImage alpha3DImg = render3DImage( frameTime, coords, alphaRect, PluginUtils.getScreenSize() );
		combineAlpha( rendered3DImg, alpha3DImg );
		
		//--- draw on provided Graphics2D object.
		g.drawImage( rendered3DImg, 0, 0, null );
	}

	public AbstractPluginEditLayer getPluginEditLayer()
	{
		return new FileSource3DPluginEditLayer( this );
	}

}//end class
